﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using NetBay.Core.Log;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Enum;
using System.Data;
using Rio.Doc.DataAccess;
#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    public class OcrMaskTranslator
    {
        /// <summary>
        /// Translates the IDataReader to a List of Mask.
        /// </summary>
        /// <param name="p_oIDataReader">p_oIDataReader</param>
        /// <returns></returns>
   
        public static List<OcrMask> TranslateMask(IDataReader p_oIDataReader)
        {
            var maskCollection = new List<OcrMask>();

            using (p_oIDataReader)
            {
                //Get Regions for each mask
                while (p_oIDataReader.Read())
                {
                    //General Informations
                    var maskID = Convert.ToInt32(p_oIDataReader["OCR_MASK_ID"]);
                    var maskLibelle = Convert.ToString(p_oIDataReader["OCR_MASK_LIBELLE"]);

                    //Author Informations
                    var authorArea = new OcrArea()
                                             {
                                                 Name =
                                                     p_oIDataReader["OCR_MASK_DOC_AUT_ZONE_NAME"] ==
                                                     DBNull.Value
                                                         ? String.Empty
                                                         : Convert.ToString(
                                                             p_oIDataReader["OCR_MASK_DOC_AUT_ZONE_NAME"]),
                                                 X =
                                                     p_oIDataReader["OCR_MASK_DOC_AUT_X"] == DBNull.Value
                                                         ? -1
                                                         : Convert.ToDouble(
                                                             p_oIDataReader["OCR_MASK_DOC_AUT_X"]),
                                                 Y =
                                                     p_oIDataReader["OCR_MASK_DOC_AUT_Y"] == DBNull.Value
                                                         ? -1
                                                         : Convert.ToDouble(
                                                             p_oIDataReader["OCR_MASK_DOC_AUT_Y"]),
                                                 Height =
                                                     p_oIDataReader["OCR_MASK_DOC_AUT_HEIGHT"] == DBNull.Value
                                                         ? -1
                                                         : Convert.ToDouble(
                                                             p_oIDataReader["OCR_MASK_DOC_AUT_HEIGHT"]),
                                                 Width =
                                                     p_oIDataReader["OCR_MASK_DOC_AUT_WIDTH"] == DBNull.Value
                                                         ? -1
                                                         : Convert.ToDouble(
                                                             p_oIDataReader["OCR_MASK_DOC_AUT_WIDTH"]),
                                             };


                    //Chrono Number Informations
                    var chronoNumberArea = new OcrArea()
                                                   {
                                                       Name =
                                                           p_oIDataReader["OCR_MASK_DOC_CHR_ZONE_NAME"] ==
                                                           DBNull.Value
                                                               ? String.Empty
                                                               : Convert.ToString(
                                                                   p_oIDataReader["OCR_MASK_DOC_CHR_ZONE_NAME"]),
                                                       X =
                                                           p_oIDataReader["OCR_MASK_DOC_CHR_X"] == DBNull.Value
                                                               ? -1
                                                               : Convert.ToDouble(
                                                                   p_oIDataReader["OCR_MASK_DOC_CHR_X"]),
                                                       Y =
                                                           p_oIDataReader["OCR_MASK_DOC_CHR_Y"] == DBNull.Value
                                                               ? -1
                                                               : Convert.ToDouble(
                                                                   p_oIDataReader["OCR_MASK_DOC_CHR_Y"]),
                                                       Height =
                                                           p_oIDataReader["OCR_MASK_DOC_CHR_HEIGHT"] ==
                                                           DBNull.Value
                                                               ? -1
                                                               : Convert.ToDouble(
                                                                   p_oIDataReader["OCR_MASK_DOC_CHR_HEIGHT"]),
                                                       Width =
                                                           p_oIDataReader["OCR_MASK_DOC_CHR_WIDTH"] ==
                                                           DBNull.Value
                                                               ? -1
                                                               : Convert.ToDouble(
                                                                   p_oIDataReader["OCR_MASK_DOC_CHR_WIDTH"]),
                                                   };

                    //Regions
                    var maskItems = TranslateRegion(DA_Ocr.GetRegionByMaskId(maskID));

                    //Build Mask
                    maskCollection.Add(new OcrMask()
                                           {
                                               Identifier = maskID,
                                               Name = maskLibelle,
                                               IndexMaskItemCollection = maskItems,
                                               AuthorArea = authorArea,
                                               //AuthorOcrType = authorOcrType,
                                               ChronoNumberArea = chronoNumberArea,
                                               //ChronoNumberOcrType = chronoNumberOcrType
                                           });
                }
            }

            return maskCollection;
        }

        /// <summary>
        /// Translates the IDataReader to a List of OcrMaskItem.
        /// </summary>
        /// <param name="p_oIDataReader">The p_o db data reader.</param>
        /// <returns></returns>
        public static List<OcrMaskItem> TranslateRegion(IDataReader p_oIDataReader)
        {
            var maskItemCollection = new List<OcrMaskItem>();

            try
            {
                using (p_oIDataReader)
                {
                    //Get Regions for each mask
                    while (p_oIDataReader.Read())
                    {
                        //Get Area
                        var regionid = Convert.ToInt32(p_oIDataReader["OCR_REGION_ID"]);
                        var regionName = Convert.ToString(p_oIDataReader["OCR_REGION_NAME"]);
                        var topLeftX = Convert.ToDouble(p_oIDataReader["OCR_REGION_TOP_LEFT_X_COORD"]);
                        var topLeftY = Convert.ToDouble(p_oIDataReader["OCR_REGION_TOP_LEFT_Y_COORD"]);
                        var height = Convert.ToDouble(p_oIDataReader["OCR_REGION_HEIGHT"]);
                        var width = Convert.ToDouble(p_oIDataReader["OCR_REGION_WIDTH"]);
                        var ocrArea = new OcrArea()
                                              {
                                                  Height = height,
                                                  Identifier = regionid,
                                                  Name = regionName,
                                                  Width = width,
                                                  X = topLeftX,
                                                  Y = topLeftY
                                              };

                        //Get OcrType
                        var rawOcrTypeName = Convert.ToString(p_oIDataReader["ocr_type_name"]);
                        var ocrType = (OcrType) System.Enum.Parse(typeof (OcrType), rawOcrTypeName);

                        //Get Index
                        var indexId = Convert.ToInt32(p_oIDataReader["RP_CH_NO"]);
                        var indexName = Convert.ToString(p_oIDataReader["RP_CH_NOM"]);
                        var index = new Index()
                                          {
                                              Indentifier = indexId,
                                              IndexLabel = indexName
                                          };

                        maskItemCollection.Add(new OcrMaskItem()
                                                   {
                                                       Area = ocrArea,
                                                       Index = index,
                                                       OcrType = ocrType
                                                   });
                    }
                }
            }
            catch (Exception e)
            {
                LogManager.Instance().LogError(e.Message);
            }

            return maskItemCollection;
        }
    }
}
